//合作机构管理控制器。
Ext.define("ES.controller.CooperationController",{
    extend:"Ext.app.Controller",
    requires:["ES.store.CooperationWayStore","ES.store.StatusStore","ES.view.RowEditingEx"],
    models:["CooperationModel","CooperationMemberModel"],
    stores:["CooperationWayStore","StatusStore","CooperationStore","CooperationMemberStore"],
    views:["cooperation.CooperationList","cooperation.CooperationEdit"],
    refs:[],
    init:function(){
            this.control({
                   "CooperationTabList":{
                       afterrender:function(cmp){
                           var btn = cmp.down("toolbar button[action=btnQuery]");
                           this.onSearch(btn);
                       },
                       itemdblclick:this.onEdit
                   },
                    "CooperationTabList button[action=btnQuery]":{
                        click:this.onSearch
                    },
                    "CooperationTabList button[action=btnAdd]":{
                        click:this.onAdd
                    },
                   "CooperationTabList button[action=btnDelete]":{
                       click:this.onDelete
                   },
                   "cooperationEdit button[action=btnSave]":{
                       click: this.onSave
                   },
                   "cooperationEdit button[action=btnCancel]":{
                       click:function(btn){
                            var win = btn.up("cooperationEdit");
                            if(win){
                                win.close();
                            }
                       }
                   }
            });
    },
    onSearch:function(btn){
        var toolbar =  btn.ownerCt;
        var grid = toolbar.ownerCt;       
        var st = grid.getStore();
        st.loadPage(1,{
            params:{
                cooperationName:toolbar.getComponent(1).getValue(),
                telPhone:toolbar.getComponent(4).getValue()
            }
        });
    },
    onAdd:function(){
        var win = Ext.widget("cooperationEdit");
        if(win){ 
            win.show(); 
        }
    },
    onEdit:function(view, record, item,index,e,opt){
        var win = Ext.widget("cooperationEdit");
        if(win){ 
            win.show();
            var el = win.getEl(); 
            el.mask("正在加载数据，请稍后....");
            if(record){
                var frm = win.down("form");
                if(frm){
                    frm.loadRecord(record);
                    var grid = win.down("gridpanel");
                    var st = grid.getStore();
                    st.loadPage(1,{
                                     params:{
                                         CooperationID:record.get("cooperationID")
                                     }
                                 });
                }
            }
            el.unmask(); 
        }
    },
    onSave:function(btn){
        var win = btn.up("cooperationEdit");
        var form = btn.up("form").getForm();
        if(form.isValid()){
              var store = this.getCooperationStoreStore();
              var record = form.getRecord();
              var values = form.getValues();
              if(record){
                  record.set(values);
              }else{
                  record = this.getCooperationModelModel().create();
                  record.set(values);
                  store.insert(0,record);
              }
              var grid = win.down("gridpanel");
              var members = grid.getStore();
              if(members.getCount() > 0){
                  var id= record.get("cooperationID");
                  members.each(function(model){
                       model.set("cooperationID",id);
                      return true;
                  })
              }
              store.sync();
              win.close();
        }
    },
    onDelete:function(btn){
        Ext.MessageBox.confirm("删除提示","确认要删除选中的数据吗？", function(cfm){
            if(cfm == "yes"){
                var grid = btn.ownerCt.ownerCt;
                var data = grid.getSelectionModel().getSelection();
                if(data.length == 0){
                        Ext.Msg.alert("提示","您最少要选择一条数据!");
                }else{
                        var st = grid.getStore();
                        Ext.Array.each(data, function(record){
                            st.remove(record);
                        })
                        st.sync();
                }
            }
        });
    }
})